package one.stand.controller;

import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import io.swagger.annotations.ApiOperation;
import com.jmp.api.dto.UserAuthListResponse;
import one.stand.dto.ConvertUtils;
import one.stand.model.Menu;
import one.stand.model.ResultModel;
import one.stand.service.user.MenuService;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author cjr
 */
@RequestMapping("crmApi/user")
@RestController
public class UserController {
    @Autowired
    private MenuService menuService;

    @PostMapping("/auth/list")
    @ApiOperation(value = "权限列表")
    @SecurityAuthCheck
    public ResultModel<UserAuthListResponse> authList() {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();

        List<Menu> openMenu = menuService.getOpenMenu(optUser.getUserId(), MenuService.TYPE_CRM_APP);
        List<String> openMenuCodes = ConvertUtils.list(openMenu, Menu::getPage);
        UserAuthListResponse response = UserAuthListResponse.builder()
                .auths(openMenuCodes)
                .build();
        return ResultModel.success(response);
    }
}
